package com.zzptc.lzh.myappone;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.lzh.myappone.Service.MyAppService;
import com.zzptc.lzh.myappone.adapter.MyRecyclerViewAdapter;
import com.zzptc.lzh.myappone.databinding.ActivityMainBinding;
import com.zzptc.lzh.myappone.entity.RecipeModel;
import com.zzptc.lzh.myappone.entity.ResultsBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);//与那个页面关联
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this);
        binding.rvRecipe.setLayoutManager(linearLayoutManager);
        //创建一个方法，数据源
        getAllRecipe();
    }

    private void getAllRecipe() {
        MyAppService myAppService = retrofit.create(MyAppService.class);
        Call<RecipeModel>call=myAppService.getAll(10);
        call.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultsBean>list;
                RecipeModel re=response.body();
                list=re.getResults();

                MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(list, MainActivity.this);
                binding.rvRecipe.setAdapter(myRecyclerViewAdapter);

            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.e(TAG, "onFailure: ",t);

            }
        });

    }
}
